<title>风险行业归类</title>

<div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">主页</a>
        <a><cite>用户</cite></a>
        <a><cite>风险行业归类</cite></a>
    </div>
</div>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-risk" data-type="batchdel">删除</button>
                <button class="layui-btn layuiadmin-btn-risk" data-type="add">添加</button>
                <button type="button" class="layui-btn layuiadmin-btn-risk" id="risks" style="margin-right: 10px">批量添加
                </button>
                <button class="layui-btn layuiadmin-btn-risk" data-type="save">保存</button>
                <button class="layui-btn layuiadmin-btn-risk" lay-filter="LAY-risk-front-search" data-type="reload"
                >
                    <!--                    <i class="layui-icon  layuiadmin-button-btn">&#xe669;</i>-->
                    刷新
                </button>
                <button class="layui-btn layuiadmin-btn-risk" lay-filter="LAY-risk-front-search" data-type="mohu"
                        style="float: right;margin: 0">
                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                </button>

                <input  style="width: 200px; float: right" type="text" onclick=""
                       name="riskinfo"
                       value="" placeholder="请输入要查询的内容" autocomplete="off" class="layui-form layui-input">

            </div>

            <table id="Risk-Industry" lay-filter="Risk-Industry"></table>

            <script type="text/html" id="buttonTpl">
                {{#  if(d.check == true){ }}
                <button class="layui-btn layui-btn-xs">已审核</button>
                {{#  } else { }}
                <button class="layui-btn layui-btn-primary layui-btn-xs">未审核</button>
                {{#  } }}
            </script>
            <script type="text/html" id="table-riskindustry">
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>
            </script>
        </div>
    </div>
</div>

<script>
    layui.use(['form', 'table', 'admin', 'view', 'upload'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table
            , admin = layui.admin
            , view = layui.view
            , upload = layui.upload;

        upload.render({
            elem: '#risks'
            , url: 'http://127.0.0.1:8000/receive_file/'
            , accept: 'file' //普通文件
            , done: function (res) {
                console.log(res)
            }
        });
        table.set({
            headers: { //通过 request 头传递
                Authorization: layui.data('layuiAdmin').Authorization
            }
        });
        table.render({
            elem: '#Risk-Industry'
            , url: './json/basicinformation/riskindustry.js' //模拟接口
            , toolbar: '#risk-tool'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'NSRSBH', width: 80, title: '纳税人识别号', sort: true,}
                , {field: 'NSRMC', title: '纳税人名称', edit: 'text'}
                , {field: 'FXHY_DM', title: '风险行业代码', edit: 'text'}
                , {field: 'FXHY_MC', title: '风险行业名称', edit: 'text'}
                , {field: 'YXBZ', title: '有效标志', edit: 'text'}
                , {field: 'YXQQ', title: '有效期起', edit: 'text'}
                , {field: 'YXQZ', title: '有效期止', edit: 'text'}
                , {field: 'LRR_DM', title: '录入人', }
                , {field: 'LRRQ', title: '录入日期',}
                , {field: 'XGR_DM', title: '修改人', }
                , {field: 'XGRQ', title: '修改日期',}
                , {fixed: 'right', title: '归类处理'}]]
            , page: true
            , height: 'full-320'
            , limit: 10
            , text: '对不起，加载出现异常！'
        });

        //头工具栏事件
        var editlis = []
        table.on('edit(Risk-Industry)', function (obj) {
            debugger
            var value = obj.value //得到修改后的值
                , data = obj.data //得到所在行所有键值
                , field = obj.field//得到字段
                , a = true; // 局部变量判断

            if (editlis.length == 0) {
                editlis.push(data)
            }
            for (let i = 0; i < editlis.length; i++) {
                if (data.NSRSBH == editlis[i].NSRSBH) {
                    a = false
                    editlis[i] = data
                }
            }
            if (a) {
                editlis.push(data)
            }
        });


        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('Risk-Industry')
                    , checkData = checkStatus.data  //得到选中的数据
                    , delobjs = []
                ;
                for (let i = 0; i < checkData.length; i++) {
                    delobjs.push(checkData[i].nsrsbh)
                }
                console.log(delobjs)
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                layer.confirm('确定删除吗？', function (index) {

                    //执行 Ajax 后重载

                    $.ajax({
                        url: `http://10.0.23.97:8195/portal/role/role`,
                        dataType: 'json',
                        data: JSON.stringify(delobjs),
                        async: false,
                        beforeSend: function (XMLHttpRequest) {
                            XMLHttpRequest.setRequestHeader("Authorization", layui.data('layuiAdmin').Authorization);
                            XMLHttpRequest.setRequestHeader("Content-Type", "application/json");
                        },
                        type: 'DELETE',
                        success: function (res) {
                            console.log("删除结果====" + JSON.stringify(res))
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            //通常情况下textStatus和errorThrown只有其中一个包含信息
                            console.log(textStatus);   //调用本次ajax请求时传递的options参数
                            console.log(errorThrown);   //调用本次ajax请求时传递的options参数
                        }

                    })

                    table.reload('LAY-user-back-role');
                    layer.msg('已删除');
                });
            },
            add: function () {
                //发请求获取所有权限数据，存储到sessionstorage中
                admin.popup({
                    title: '添加风险行业'
                    , area: ['600px', '700px']
                    // , id: 'LAY-popup-user-add'
                    , success: function (layero, index) {
                        view(this.id).render('DataAnalysisWarning/BasicInformationCollection/risk/addrisk').done(function () {
                            form.render(null, 'layuiadmin-form-risk');

                            //监听提交
                            form.on('submit(LAY-risk-submit)', function (data) {
                                var field = data.field; //获取提交的字段

                                console.log("添加角色===" + JSON.stringify(field))

                                //提交 Ajax 成功后，关闭当前弹层并重载表格
                                // $.ajax({
                                //     url: "http://10.0.23.97:8195/portal/role/role",
                                //     data: field,
                                //     dataType: "json",
                                //     async: false,
                                //     beforeSend: function (XMLHttpRequest) {
                                //         XMLHttpRequest.setRequestHeader("Authorization", layui.data('layuiAdmin').Authorization);
                                //     },
                                //     type: 'POST',
                                //     success: function (res) {
                                //         console.log("")
                                //     },
                                //     error: function (XMLHttpRequest, textStatus, errorThrown) {
                                //         //通常情况下textStatus和errorThrown只有其中一个包含信息
                                //         console.log(textStatus);   //调用本次ajax请求时传递的options参数
                                //         console.log(errorThrown);   //调用本次ajax请求时传递的options参数
                                //     }
                                // })
                                layui.table.reload('Risk-Industry'); //重载表格
                                layer.close(index); //执行关闭
                            });
                        });
                    }
                });
            },
            mohu: function () {
                debugger
                console.log('进来')
                var riskVal = $("input[name='riskinfo']").val()
                console.log(riskVal)

                //执行重载
                table.reload('Risk-Industry', {
                   page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        keyword:riskVal
                    }
                });
            },
            save: function () {
                alert(JSON.stringify(editlis))
            },
            reload:function () {
                table.reload('Risk-Industry');
            }
        }
        $('.layui-btn.layuiadmin-btn-risk').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    })

</script>